<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script>
      run(async function () {
        const {
          WebChat: { testIds }
        } = window;

        const clock = lolex.createClock();
        const { directLine, store } = testHelpers.createDirectLineEmulator({ ponyfill: clock });

        WebChat.renderWebChat(
          {
            directLine,
            store
          },
          document.getElementById('webchat')
        );

        await pageConditions.uiConnected();

        await directLine.emulateIncomingActivity({
          entities: [
            {
              '@context': 'https://schema.org',
              '@type': 'VoteAction',
              type: 'https://schema.org/VoteAction',
              actionOption: 'upvote'
            },
            {
              '@context': 'https://schema.org',
              '@type': 'VoteAction',
              type: 'https://schema.org/VoteAction',
              actionOption: 'downvote'
            }
          ],
          text: 'Hello, World!',
          timestamp: new Date().toISOString(),
          type: 'message'
        });

        await pageConditions.numActivitiesShown(1);

        await host.snapshot('local');

        const buttons = pageElements.allByTestId(testIds.feedbackButton);

        pageElements.sendBoxTextBox().focus();

        const upvoteResult = await directLine.actPostActivity(async () => {
          await host.click(buttons[0]);

          // Wait for 400 ms for the tooltip to show up.
          await new Promise(resolve => setTimeout(resolve, 400));

          await host.snapshot('local');

          clock.tick(500);
        });

        expect(upvoteResult).toHaveProperty(
          'activity',
          expect.objectContaining({
            entities: expect.arrayContaining([
              {
                '@context': 'https://schema.org',
                '@type': 'VoteAction',
                type: 'https://schema.org/VoteAction',
                actionOption: 'upvote'
              }
            ]),
            name: 'webchat:activity-status/feedback',
            type: 'event'
          })
        );

        const downvoteResult = await directLine.actPostActivity(async () => {
          await host.click(buttons[1]);

          // Wait for 400 ms for the tooltip to show up.
          await new Promise(resolve => setTimeout(resolve, 400));

          await host.snapshot('local');

          clock.tick(500);
        });

        expect(downvoteResult).toHaveProperty(
          'activity',
          expect.objectContaining({
            entities: expect.arrayContaining([
              {
                '@context': 'https://schema.org',
                '@type': 'VoteAction',
                type: 'https://schema.org/VoteAction',
                actionOption: 'downvote'
              }
            ]),
            name: 'webchat:activity-status/feedback',
            type: 'event'
          })
        );

        const switchingResult = await directLine.actPostActivity(async () => {
          await host.click(buttons[0]);
          await host.click(buttons[1]);

          // Wait for 400 ms for the tooltip to show up.
          await new Promise(resolve => setTimeout(resolve, 400));

          await host.snapshot('local');

          clock.tick(500);
        });

        expect(downvoteResult).toHaveProperty(
          'activity',
          expect.objectContaining({
            entities: expect.arrayContaining([
              {
                '@context': 'https://schema.org',
                '@type': 'VoteAction',
                type: 'https://schema.org/VoteAction',
                actionOption: 'downvote'
              }
            ]),
            name: 'webchat:activity-status/feedback',
            type: 'event'
          })
        );
      });
    </script>
  </body>
</html>
